<?php

Raise::load('core.RaiseStaticClass');
Raise::load('core.IRaiseInitializable');
Raise::load('core.RaiseVersion');
Raise::load('core.IpAddress');

/**
 * RaiseServer - Server helper class
 * Contain information about the server
 *
 * @author Sam-Mauris Yong <hellclanner at live dot com>
 * @license http://www.opensource.org/licenses/bsd-license New BSD License
 * @package Raise.Core.Helper
 * @since 1.2
 */
class RaiseServer extends RaiseStaticClass implements IRaiseInitializable {

    /**
     * The Operating System the server is running on
     * @var string
     * @link http://php.net/php-uname
     * @static
     */
    private static $operatingSystem;

    /**
     * The version of the Operating System the server is running on
     * @var string
     * @link http://php.net/php-uname
     * @static
     */
    private static $osVersion;

    /**
     * The machine / processor type
     * @var string
     * @link http://php.net/php-uname
     * @static
     */
    private static $machineType;

    /**
     * Revision of the CGI specification the server is using e.g. CGI/1.1
     * @var string
     * @static
     */
    private static $gatewayInterface;

    /**
     * The IP Address of the server
     * @var IpAddress
     * @static
     */
    private static $ipAddress;

    /**
     * The host name of the server
     * @var string
     * @static
     */
    private static $hostname;

    /**
     * Server identification string, given in the headers when responding to requests.
     * @var string
     */
    private static $software;

    /**
     * The protocol and revision which the page was requested via. e.g. HTTP/1.1
     * @var string
     * @static
     */
    private static $protocol;

    /**
     * The value given to the SERVER_ADMIN (for Apache) directive in the web server configuration file. If the script is running on a virtual host, this will be the value defined for that virtual host.
     * @var string
     * @static
     */
    private static $admin;

    /**
     * The port which the server is currently using
     * @var string
     * @static
     */
    private static $port;

    /**
     * The server version and host name which are added to server-generated pages
     * @var string
     * @static
     */
    private static $signature;

    /**
     * Whether the server is on HTTPS or not
     * @var boolean
     * @static
     */
    private static $https;

    /**
     * Create the Server instance
     * @static
     */
     public static function initialize() {
        self::$operatingSystem = php_uname('s');
        self::$osVersion = php_uname('v');
        self::$machineType = php_uname('m');

        self::$gatewayInterface = $_SERVER['GATEWAY_INTERFACE'];
        self::$ipAddress = new IpAddress($_SERVER['SERVER_ADDR']);
        self::$hostname = $_SERVER['SERVER_NAME'];
        self::$software = $_SERVER['SERVER_SOFTWARE'];
        self::$protocol = $_SERVER['SERVER_PROTOCOL'];

        self::$admin = $_SERVER['SERVER_ADMIN'];
        self::$port = $_SERVER['SERVER_PORT'];
        self::$signature = $_SERVER['SERVER_SIGNATURE'];
        self::$https = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off');
    }

    /**
     * Get the operating system that the framework and server is running on
     * @return string
     * @static
     */
    public static function operatingSystem(){
        return $this->operatingSystem;
    }

    /**
     * Get the operating system version that the framework and server is running on
     * @return string
     * @static
     */
    public static function osVersion(){
        return $this->osVersion;
    }

    /**
     * Get the type of machine that the framework and server is running on
     * @return string
     * @static
     */
    public static function machineType(){
        return $this->machineType;
    }

    /**
     * Get the gateway interface
     * @return string
     * @static
     */
    public static function gatewayInterface(){
        return $this->gatewayInterface;
    }

    /**
     * Get the IP Address of the machine
     * @return IpAddress
     * @static
     */
    public static function ipAddress(){
        return $this->ipAddress;
    }

    /**
     * Get the hostname of the machine
     * @return string
     * @static
     */
    public static function hostname(){
        return $this->hostname;
    }

    /**
     * Get the server identification string, given in the headers when responding to requests.
     * @return string
     * @static
     */
    public static function software($s = null){
        if(func_num_args() == 1){
            $this->software = $s;
        }
        return $this->software;
    }

    /**
     * Get the protocol and revision which the page was requested via. e.g. HTTP/1.1
     * @return string
     * @static
     */
    public static function protocol(){
        return $this->protocol;
    }

    /**
     * Get the value given to the SERVER_ADMIN (for Apache) directive in the web server configuration file. If the script is running on a virtual host, this will be the value defined for that virtual host.
     * @return string
     * @static
     */
    public static function admin(){
        return $this->admin;
    }

    /**
     * Get the port number which this page is currently served through the server
     * @return string
     * @static
     */
    public static function port(){
        return $this->port;
    }

    /**
     * Get the server version and host name which are added to server-generated pages
     * @return string
     * @static
     */
    public static function signature(){
        return $this->signature;
    }

    /**
     * phpInfo
     * @link http://php.net/phpinfo
     * @static
     */
    public static function phpInfo() {
        phpinfo();
    }

    /**
     * Execute command via shell and return the complete output as a string
     * @param string $h Command to execute
     * @return string
     * @static
     */
    public static function execute($h) {
        return shell_exec($h);
    }

    /**
     * Query whether the server is on HTTPS or not
     * @return boolean
     * @static
     */
    public static function https(){
        return self::$https;
    }

    /**
     * Query PHP's version, or a particular extension if $v is provided
     * @param string $v (optional) Name of the extension to query version
     * @return Version
     * @static
     */
    public static function getVersion($v = false) {
        if ($v) {
            $r = new RaiseVersion(phpversion($v));
        } else {
            $r = new RaiseVersion(PHP_VERSION);
        }
        return $r;
    }

    /**
     * Get a System Environment variable
     * @param string $var Name of the variable to fetch
     * @return mixed
     * @static
     */
    public static function getEnvironment($var) {
        return getenv($var);
    }

    /**
     * Set a System Environment variable
     * @param string $var Name of the variable to set
     * @param mixed $value Value of the variable
     * @return boolean
     * @static
     */
    public static function setEnvironment($var,$value) {
        return putenv($var.'='.$value);
    }

    /**
     * Get the process ID of PHP
     * @return integer
     * @link http://php.net/getmypid
     */
    public static function processID() {
        return getmypid();
    }

}